package com.wisely.highlight_spring4.ch3.aware;

import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;
/**
 * 实现BeanNameAware, ResourceLoaderAware接口，获得Bean名称和资源加载的服务
 * @author dell
 *
 */
@Service
public class AwareService implements BeanNameAware, ResourceLoaderAware {
	
	private String name;
	private ResourceLoader loader;

	@Override
	public void setResourceLoader(ResourceLoader loader) {
		// TODO Auto-generated method stub
		this.loader = loader;
	}

	@Override
	public void setBeanName(String name) {
		// TODO Auto-generated method stub
		this.name = name;
	}
	
	public void outputResult(){
		System.out.println("Bean的名称："+name);
		Resource resource = loader.getResource("classpath:com/wisely/highlight_spring4/ch3/aware/test.txt");
		try {
			System.out.println("ResourceLoader加载的文件内容："+IOUtils.toString(resource.getInputStream()));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
